﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SharedLib.XmlDataModel;
using System.Xml;

namespace Tracy.BasicServices
{
    public class UserPreferenceManager
    {
        UserPreference _pref;
        private const string FILENAME = "UserPreferences.xml";
        public UserPreferenceManager()
        {
            XmlNode node = null;
            if (File.Exists(FILENAME))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(FILENAME);
                node = doc.SelectSingleNode("/user-preference");
            }

            if (node == null)
            {
                _pref = new UserPreference();
                _pref.Properties["save-path"] = Tracy.Properties.Settings.Default.DefaultDownloadFolder;
            }
            else
            {
                _pref = new UserPreference(node);
            }
        }

        class UserPreference : XmlDataModelWithAttributes
        {
            public UserPreference(XmlNode node) : base(node) { }
            public UserPreference() : base() { }

            public override string RootNodeName
            {
                get
                {
                    return "user-preference";
                }
            }

        }

        public string SavePath
        {
            get
            {
                string rtn = _pref.Properties["save-path"];
                if (rtn == null) return null;
                if (!rtn.EndsWith("\\")) rtn += "\\";
                return rtn;
            }
            set {
                if (_pref.Properties["save-path"] != value)
                {
                    _pref.Properties["save-path"] = value;
                    _pref.ToXmlDocument().Save(FILENAME);
                }
            }
        }
    }
}
